package calendar;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

/* this class provides the panel used to create calendars */
class CreatePanel extends CalendarPanel implements DocumentListener {

  private static final long serialVersionUID = 34734783L;

  String userName;
  TextInputPanel userNameTextPanel;
  JButton createButton;

  public CreatePanel()
  {
    Dimension userNameTextPanelSize = new Dimension( 210, 20 );

    userNameTextPanel = new TextInputPanel( "User Name:", 50 );
    userNameTextPanel.setMaximumSize( userNameTextPanelSize );
    userNameTextPanel.setMinimumSize( userNameTextPanelSize );
    userNameTextPanel.setPreferredSize( userNameTextPanelSize );
    userNameTextPanel.addDocumentListener( this );

    Dimension createButtonSize = new Dimension( 75, 20 );

    createButton = new JButton( "Create" );
    createButton.setMaximumSize( createButtonSize );
    createButton.setMinimumSize( createButtonSize );
    createButton.setPreferredSize( createButtonSize );
    createButton.setToolTipText( "Create a new Calendar" );
    createButton.addActionListener( this );

    setLayout( new BoxLayout( this, BoxLayout.X_AXIS ) );

    add( Box.createHorizontalStrut( 5 ) );
    add( userNameTextPanel );
    add( Box.createHorizontalStrut( 5 ) );
    add( createButton );
  }

  public String getUserName()
  {
    return userName;
  }

  public void clearText()
  {
    userNameTextPanel.clear();
  }

  public void setFocusCreate()
  {
    getRootPane().setDefaultButton( createButton );
  }  

  public void actionPerformed(ActionEvent e)
  {
    int eventId = -1;
    if ( e.getSource() == createButton )
    {
      eventId = CalEvent.CREATE;
      userName = userNameTextPanel.getText();
    }
    else return;

    CalEvent calEvent = new CalEvent( this, eventId );
    fireCalEvent( calEvent );
  }

  public void insertUpdate(DocumentEvent e)
  {
    int eventId = CalEvent.FOCUS_CREATE;
    CalEvent calEvent = new CalEvent( this, eventId );
    fireCalEvent( calEvent );
  }

  public void removeUpdate(DocumentEvent e)
  {
    int eventId = CalEvent.FOCUS_CREATE;
    CalEvent calEvent = new CalEvent( this, eventId );
    fireCalEvent( calEvent );
  }

  public void changedUpdate(DocumentEvent e)
  {
    throw new UnsupportedOperationException("Not supported yet.");
  }
}
